var exp = module.exports;

exp.reward = [{
    name: '大量金币',
    key: 'no1',
    radio: 15,
}, {
    name: '海量金币',
    key: 'no2',
    radio: 10,
}, {
    name: '中量金币',
    key: 'no3',
    radio: 20,
}, {
    name: '加速3分钟',
    key: 'no4',
    radio: 10
}, {
    name: '少量金币',
    key: 'no5',
    radio: 35
}, {
    name: '抽奖券x2',
    key: 'no6',
    radio: 10
}];


exp.Draw = function (prizes) {
    var prizeList = [] //按照权重分解后的奖品数组
    prizes.map(function (item) {
        prizeList.push({
            name: item.name,
            value: item.key
        })
        for (var i = 0; i < item.radio; i++) {
            prizeList.push({
                name: item.name,
                value: item.key
            })
        }
    });
    prizeList = reset(prizeList);

    // 范围随机数
    function randomFrom(lowerValue, upperValue) {
        return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
    };

    // 随机打乱数组
    function reset(arr) {
        var eachArr = arr.concat([])
        var lastArr = []

        function deepEach(deepArr) {
            if (deepArr.length) {
                var randomIndex = randomFrom(0, eachArr.length - 1)
                lastArr.push(eachArr[randomIndex])
                eachArr.splice(randomIndex, 1)
                deepEach(eachArr)
            }
        }

        deepEach(eachArr)
        return lastArr
    }

    this.getResult = function () {
        var random = randomFrom(0, prizeList.length - 1);
        return prizeList[random]
    }
}
